﻿using System;
using System.Collections.Generic;
using System.Transactions;
using Bingosoft.Data;
using HHMMS.Core.Model;

namespace HHMMS.Core.Service
{
    public class PurchasePlanDocService
    {
        private readonly Dao _dao = Dao.Get();

        public PurchasePlanDoc GetPurchasePlanDoc(Int32 docId)
        {
            var param = new Dictionary<string, object>() { { "DocId", docId } };
            return this._dao.QueryEntity<PurchasePlanDoc>("PlanManager.PurchasePlanDoc.Get", param);                
        }

        public int SaveOrUpdatePurchasePlanDoc(PurchasePlanDoc purchasePlanDoc)
        {
            using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.Required))
            {
                if (!purchasePlanDoc.DocId.HasValue)
                {
                    purchasePlanDoc.DocId = this._dao.QueryScalar<int>("PlanManager.PurchasePlanDoc.Insert", purchasePlanDoc);
                }
                else
                {
                    this._dao.ExecuteNonQuery("PlanManager.PurchasePlanDoc.Update", purchasePlanDoc);
                }
                transactionScope.Complete();
            }
            return purchasePlanDoc.DocId ?? 0;
        }

        public void DeletePurchasePlanDoc(string docId)
        {
            using (TransactionScope transactionScope = new TransactionScope())
            {
                var param = new Dictionary<string, object>() { { "DocId", docId } };
                this._dao.ExecuteNonQuery("PlanManager.PurchasePlanDoc.Delete", param);
                transactionScope.Complete();
            }

        }
        
    }
}